/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package suit.ui;

/**
 * A ToggleButton is a Button which retains its state.
 * 
 * @author Devin Samarin
 * 
 */
public class ToggleButton extends Button {

	/**
	 * Constructs a new ToggleButton
	 */
	public ToggleButton() {
	}

	/**
	 * Constructs a new ToggleButton with a label.
	 * 
	 * @param text
	 *            the text for the Label
	 */
	public ToggleButton(String text) {
		super(text);
	}

	protected boolean onPointerPressed(Event e) {
		if (localizeEvent(e) != null) {
			setActive(!active);
			invalidate(this);
			if (toggleHandler != null)
				toggleHandler.onToggled(this);
		}
		return false;
	}

	protected boolean onPointerReleased(Event e) {
		if (localizeEvent(e) != null) {
			if (clickHandler != null)
				clickHandler.onClicked(this);
		}
		return false;
	}

	protected Toggled toggleHandler;

	/**
	 * Hook up a ToggleButton.Toggled signal handler.
	 * 
	 * @param handler
	 *            the signal handler to hook up
	 */
	public void connect(Toggled handler) {
		toggleHandler = handler;
	}

	/**
	 * Signal indicating the Button has changed state.
	 * 
	 * @author Devin Samarin
	 * 
	 */
	public interface Toggled {

		public void onToggled(ToggleButton source);
	}

}
